package wangyachuan.sptingboot.controller;

import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.*;
import wangyachuan.sptingboot.common.AuthAccess;
import wangyachuan.sptingboot.common.Result;
import wangyachuan.sptingboot.model.user;
import wangyachuan.sptingboot.server.UserServer;

@SuppressWarnings("all")
@RestController
public class WebController {

    @Resource
    UserServer userServer;

    @AuthAccess
    @GetMapping
    public Result hello() {
        return Result.success("success");
    }

    @PostMapping("/login")
    public Result login(@RequestBody user user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {//判断用户名和密码是否为空
            return Result.error("用户名或密码不能为空");
        }
        user = userServer.login(user);//调用服务层方法进行登录验证
        return Result.success(user);//返回登录成功的用户信息
    }

    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody user user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {//判断用户名和密码是否为空
            return Result.error("用户名或密码不能为空");
        }
        if (user.getUsername().length() > 10 || user.getPassword().length() > 20) {//
            return Result.error("用户名或密码不能为空");
        }
        user = userServer.register(user);//调用服务层方法进行登录验证
        return Result.success(user);//
    }
}
